package com.ebelter.ty.ui.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.base.BlueManager;
import com.ebelter.ty.R;
import com.ebelter.ty.model.AMesuresCalBack;
import com.ebelter.ty.model.TyManager;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B1_Fragment;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B2_Fragment;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B3_Fragment;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B4_Fragment;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B5_Fragment;
import com.ebelter.ty.utils.TyUtils;

import butterknife.BindView;

public class ScaleMainActivity extends TyBaseActivity implements View.OnClickListener {



    LinearLayout nopairedtiply_root_ll;//没有绑定蓝牙设备的根布局
    TextView nopairedtiply_pair_now_tv;//

    LinearLayout addinfo_root_ll;//添加信息的根布局
    TextView addinfo_pair_now_tv;//

    private LinearLayout scale_main_b_1_ll;
    private LinearLayout scale_main_b_2_ll;
    private LinearLayout scale_main_b_3_ll;
    private LinearLayout scale_main_b_4_ll;
    private LinearLayout scale_main_b_5_ll;

    private Scale_M_B1_Fragment scale_M_B1_Fragment;
    private Scale_M_B2_Fragment scale_M_B2_Fragment;
    private Scale_M_B3_Fragment scale_M_B3_Fragment;
    private Scale_M_B4_Fragment scale_M_B4_Fragment;
    private Scale_M_B5_Fragment scale_M_B5_Fragment;

    private TyManager mTyManager;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_scale_main;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        scale_main_b_1_ll = findViewById(R.id.scale_main_b_1_ll);
        scale_main_b_2_ll = findViewById(R.id.scale_main_b_2_ll);
        scale_main_b_3_ll = findViewById(R.id.scale_main_b_3_ll);
        scale_main_b_4_ll = findViewById(R.id.scale_main_b_4_ll);
        scale_main_b_5_ll = findViewById(R.id.scale_main_b_5_ll);
        nopairedtiply_root_ll = findViewById(R.id.nopairedtiply_root_ll);
        addinfo_root_ll = findViewById(R.id.addinfo_root_ll);
        nopairedtiply_pair_now_tv = findViewById(R.id.nopairedtiply_pair_now_tv);
        addinfo_pair_now_tv = findViewById(R.id.addinfo_pair_now_tv);

        scale_main_b_1_ll.setOnClickListener(this);
        scale_main_b_2_ll.setOnClickListener(this);
        scale_main_b_3_ll.setOnClickListener(this);
        scale_main_b_4_ll.setOnClickListener(this);
        scale_main_b_5_ll.setOnClickListener(this);
        nopairedtiply_pair_now_tv.setOnClickListener(this);
        addinfo_pair_now_tv.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        buttomSelect(3);

        mTyManager = new TyManager(this);
        mTyManager.setMesuresCalBack(new AMesuresCalBack());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTyManager.startWord();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTyManager.stopWord();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.scale_main_b_1_ll:
                buttomSelect(1);
                break;
            case R.id.scale_main_b_2_ll:
                buttomSelect(2);
                break;
            case R.id.scale_main_b_3_ll:
                buttomSelect(3);
                break;
            case R.id.scale_main_b_4_ll:
                buttomSelect(4);
                break;
            case R.id.scale_main_b_5_ll:
                buttomSelect(5);
                break;
            case R.id.nopairedtiply_pair_now_tv:
                if (BlueManager.getInstance().isBluetoothOpen()){
                    Intent mIntent1 = new Intent(this, BindDeviceActivity.class);
                    startActivity(mIntent1);
                }else {
//                    Intent mIntent2 = new Intent(this, BindDeviceHintActivity.class);
//                    startActivity(mIntent2);
                }
                break;
            case R.id.addinfo_pair_now_tv:
//                Intent intent=new Intent(ScaleMainActivity.this,AddUserInfoActivity.class);
//                startActivityForResult(intent,AddUserInfoActivity.REQUEST_CODE);
                break;


        }
    }

    private void buttomSelect(int i) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideAllFragment(transaction, scale_M_B1_Fragment, scale_M_B2_Fragment, scale_M_B3_Fragment, scale_M_B4_Fragment, scale_M_B5_Fragment);//隐藏全部的fragment

        int contain = R.id.scale_main_fl;//容器

        TyUtils.setLinearLayoutChildSelects(false,scale_main_b_1_ll,scale_main_b_2_ll,scale_main_b_3_ll,scale_main_b_4_ll,scale_main_b_5_ll);
        if (i==1){
            TyUtils.setLinearLayoutChildSelect(scale_main_b_1_ll,true);

            if (scale_M_B1_Fragment == null) {
                scale_M_B1_Fragment = new Scale_M_B1_Fragment();
                transaction.add(contain, scale_M_B1_Fragment);
            } else {
                transaction.show(scale_M_B1_Fragment);
            }

        }else if (i==2){
            TyUtils.setLinearLayoutChildSelect(scale_main_b_2_ll,true);
            if (scale_M_B2_Fragment == null) {
                scale_M_B2_Fragment = new Scale_M_B2_Fragment();
                transaction.add(contain, scale_M_B2_Fragment);
            } else {
                transaction.show(scale_M_B2_Fragment);
            }
        }else if (i==3){
            TyUtils.setLinearLayoutChildSelect(scale_main_b_3_ll,true);
            if (scale_M_B3_Fragment == null) {
                scale_M_B3_Fragment = new Scale_M_B3_Fragment();
                transaction.add(contain, scale_M_B3_Fragment);
            } else {
                transaction.show(scale_M_B3_Fragment);
            }
        }else if (i==4){
            TyUtils.setLinearLayoutChildSelect(scale_main_b_4_ll,true);
            if (scale_M_B4_Fragment == null) {
                scale_M_B4_Fragment = new Scale_M_B4_Fragment();
                transaction.add(contain, scale_M_B4_Fragment);
            } else {
                transaction.show(scale_M_B4_Fragment);
            }
        }else if (i==5){
            TyUtils.setLinearLayoutChildSelect(scale_main_b_5_ll,true);
            if (scale_M_B5_Fragment == null) {
                scale_M_B5_Fragment = new Scale_M_B5_Fragment();
                transaction.add(contain, scale_M_B5_Fragment);
            } else {
                transaction.show(scale_M_B5_Fragment);
            }
        }

        transaction.commit();
    }


    //更新趋势界面目标体重
    public void trendFragmentUpdateTargetWeight() {
    }

    //返回蓝牙体脂秤是否打开的标志
    public boolean scalebleIsConnect() {
        return mTyManager!=null&&mTyManager.isConnected();
    }

    //请求删除体脂秤的用户信息
    public void requestDelAllUser() {
        if (mTyManager!=null){
            mTyManager.sendUserInfo2Scale();
        }
    }
}
